using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
namespace XProject.GUI
{
    public partial class ImageBrowser : Form
    {
    
        public string ImagePath
        {
            get { return txtFileName.Text; }
            set { txtFileName.Text = value; }
        }
        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }
        public ImageBrowser()
        {
            InitializeComponent();
        }

        private void ImageBrowser_Load(object sender, EventArgs e)
        {
            if (ImagePath != "")
                if (File.Exists(Application.StartupPath+"\\emotions\\"+ ImagePath))
                    picImage.Image = Image.FromFile(Application.StartupPath+"\\emotions\\"+ ImagePath);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {                
                OpenFileDialog dlgOpen = new OpenFileDialog();
                string path = Application.StartupPath;
                path = path + (path.EndsWith("\\") ? "" : "\\");
                path = path + ConfigurationManager.AppSettings["dir_emotions"];
                path = path + (path.EndsWith("\\") ? "" : "\\");
                dlgOpen.InitialDirectory = path;
                dlgOpen.Filter = "Supported Images(*.png, *.ico, *.gif)|*.png;*.ico;*.gif";
                if (dlgOpen.ShowDialog() == DialogResult.OK)
                {
                    if (!dlgOpen.FileName.Contains(path))
                    {
                        MessageBox.Show("Only images in the folder "+path+" is accepted.\nPlease try again");
                        return;
                    }
                    picImage.Image = Image.FromFile(dlgOpen.FileName);
                    txtFileName.Text = dlgOpen.FileName.Replace(path,"");
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ;}
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}